<?php
$this->Html->addCrumb('Users', '/admin/users');
$this->Html->addCrumb('Edit');
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
        $("#UserAdminEditForm").TWS_Form( {
          validate : {
          rules : {
            UserFirstName : {required : true},
            UserLastName : {required : true},
            UserVerified : {help : "<strong>Yes</strong> - User email address was verified<br/><br/><strong>No</strong> - User email address has not yet been verified.<br/><br/><strong>Please Note:</strong> This should not be changed. Only in rare cases when a user is unable to get a verification email upon registration should this be changed."},
            UserActive : {help : "<strong>Yes</strong> - User will be able to login to restricted areas of the website<br/><br/><strong>No</strong> - User will not be able to login to restricted website areas until the account is set Active again"},
            UserRoles : {help : "Select at least one role for the user", required : true}
          }
          }});

//TODO: Verify that at least one role is checked
       
         $("#deleteBtn").click(function(event){
          if(!$("#deleteBtn").attr("data-delete-confirm")) {
          $("#deleteBtn").attr("data-delete-confirm","");
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this record?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); $("#deleteBtn").attr("data-delete-confirm",true).trigger("click");}}}});
            event.preventDefault();
          }
        });
      });
       ';
$this->Html->scriptEnd();

//getting the current user roles
if (isset($this->request->data)) {
  $userRoles = array();
  foreach($this->request->data['Role'] as $currRole) {
    array_push($userRoles, $currRole['role_name']);
  }
}
?>


<h1>Edit User</h1>
<?php echo $this->Session->flash();?>
<?php echo '<div id="actionLink">'.$this->Html->link('Back to list of Users',array('action'=>'index', 'admin'=>true)).'</div>'; ?>

<?php echo $this->element('errors', array('errors' => isset($errors)? $errors : null)); ?>
<?php

echo $this->TWSForm->create('User',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo $this->TWSForm->input('first_name', array('name'=>'UserFirstName', 'type'=>'text',  'size'=>'40','label'=>'<span class="required">*</span>First name'));
echo $this->TWSForm->input('last_name', array('name'=>'UserLastName', 'type'=>'text',  'size'=>'40','label'=>'<span class="required">*</span>Last name'));
echo'<p>Email: '.$this->request->data['User']['email'].'</p>';
echo $this->TWSForm->input('verified', array('type'=>'select','options'=>$verificationOptions,'name'=>'UserVerified'));
echo $this->TWSForm->input('active', array('type'=>'select','options'=>$activeOptions,'name'=>'UserActive'));
?>
<div class="row">
  <fieldset id="UserRoles">
    <legend>User roles</legend>
    <?php
      $count = 0;
      foreach ($roles as $role) {
        $checked ="";
        if (in_array($role['Role']['role_name'], $userRoles)) { $checked = "checked";}
        echo "<label class='stacked' for='role$count'><input id='role$count' name='role$count' type='checkbox' value='".$role['Role']['role_name']."'  $checked  />".$role['Role']['role_name']."</label>";
        $count++;
      }
    ?>
  </fieldset>
</div>
<?php
echo $this->TWSForm->submit('Update', array('id'=>'editBtn', 'name'=>'editBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->submit("Delete", array('id'=>'deleteBtn','name'=>'deleteBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->end();

?>